//
//  RootVC.m
//  AirQuality
//
//  Created by Po on 15/3/10.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RootVC.h"
#import "RJTabBarView.h"

#import "RJHomeVC.h"
#import "RJCheckVC.h"
#import "RJWarningVC.h"
#import "RJBuildingVC.h"
#import "RJOutVC.h"
#import "SetVC.h"

#import "RJLoginVC.h"
#import "RJUserData.h"

@interface RootVC () <RJTabBarViewDelegate>

@property (weak, nonatomic) IBOutlet RJTabBarView *tabbarView;

@property (retain, nonatomic) RJMainSuperVC * currentVC;

@property (assign, nonatomic) BOOL isInit;                  //是否已经初始化
@property (assign, nonatomic) BOOL isMoveing;               //正在移动
@property(assign, nonatomic) NSInteger flage;
- (void)initData;
- (void)initInterface;

@end

@implementation RootVC
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initData];
    }
    return self;
}


#pragma mark - user-defined initialization function
- (void)viewWillAppear:(BOOL)animated
{
    _flage = 1;
    [RJUserData defaultRJUserData].paoPaoName = @"";
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectPao:) name:@"selectPao" object:nil];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOutEvent) name:@"logOutEvent" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logInEvent) name:@"logInEvent" object:nil];
    });
    
    //内部界面
    if(![RJUserData defaultRJUserData].isLogined){
        RJLoginVC * loginVC = [[RJLoginVC alloc] init];
        [self.navigationController pushViewController:loginVC animated:NO];
        return;
    }
    if (!_isInit) {
        _isInit = YES;
        [self initInterface];
    }
}


- (void)initData
{
    _isInit = NO;
    _isMoveing = NO;
}

- (void)initInterface
{
    [_tabbarView setDelegate:self];
    [self initHomeVC];
}

- (void)initHomeVC
{
    if (_currentVC) {
        [_currentVC removeFromParentViewController];
        [_currentVC.view removeFromSuperview];
    }
    _currentVC = [[RJHomeVC alloc] init];
    //设置主页显示
    [self.view insertSubview:_currentVC.view atIndex:0];
    [self addChildViewController:_currentVC];
}

#pragma mark - function
//切换页面
- (void)changeShowVCWithCurrentPageNum:(NSUInteger)currentPageNum lastPageNum:(NSUInteger)lastPageNum
{
    _flage = 1;
    BOOL isMoveLeft = (currentPageNum > lastPageNum) ? YES : NO;
    [_currentVC playMoveAnimDirection:(isMoveLeft ? moveLeft : moveRight) isRemove:YES];
    
    RJMainSuperVC * nextVC = [self getShowVCCurrentPageNum:currentPageNum isMoveLeft:isMoveLeft];
    
    [self addChildViewController:nextVC];
    [self.view insertSubview:nextVC.view belowSubview:_currentVC.view];
    
    [nextVC playMoveAnimDirection:(isMoveLeft ? moveRight : moveLeft) isRemove:NO];
    _currentVC = nextVC;
}

//初始化并返回当前页面
- (RJMainSuperVC *)getShowVCCurrentPageNum:(NSUInteger)currentPageNum isMoveLeft:(BOOL)isMoveLeft
{
    RJMainSuperVC * newVC;
    switch (currentPageNum) {
        case 1:
            newVC = [[RJHomeVC alloc] init ];
            break;
        case 2:
            newVC = [[RJOutVC alloc] init];
            [RJUserData defaultRJUserData].isYearData = NO;
//            newVC = [[RJCheckVC alloc] init];
            break;
        case 3:
            newVC = [[RJCheckVC alloc] init];
//          newVC = [[RJWarningVC alloc] init];
            break;
        case 4:
            newVC = [[RJWarningVC alloc] init];
//            newVC = [[RJOutVC alloc] init];
//            [RJUserData defaultRJUserData].isYearData = NO;
            break;
        case 5:
            newVC = [[SetVC alloc] init];
            break;
        default:
            break;
    }
    [newVC.view setFrame:CGRectMake((isMoveLeft ? ScreenWidth : -ScreenWidth), 0, ScreenWidth, ScreenHeight)];
    return newVC;
}

#pragma mark - RJTabBarViewDelegate
- (void)RJTabBarDidSelectedPage:(NSUInteger)currentPageNum lastPageNum:(NSUInteger)lastPageNum
{
    [self changeShowVCWithCurrentPageNum:currentPageNum lastPageNum:lastPageNum];
}

#pragma mark - Notifaction
- (void)logOutEvent
{
    [[RJUserData defaultRJUserData] setLoginState:NO];
    [RJUserData defaultRJUserData].isRequestMessage = NO;
    _isInit = NO;
    RJLoginVC * loginVC = [[RJLoginVC alloc] init];
    loginVC.isLogOut = YES;
    [self.navigationController pushViewController:loginVC animated:NO];
    
    if (![_currentVC isKindOfClass:[RJHomeVC class]]) {
        [_currentVC removeFromParentViewController];
        [_currentVC.view removeFromSuperview];
        _currentVC = [[RJHomeVC alloc] init];
        [self addChildViewController:_currentVC];
        [self.view addSubview:_currentVC.view];
        [_tabbarView reSetSelectCount:1];
    }
}

- (void)selectPao:(NSNotification *)notification
{
    if (_flage == 1) {
        [self changeShowVCWithCurrentPageNum:2 lastPageNum:0];
    }
    _flage ++;
}

- (void)logInEvent
{
    if (![RJUserData defaultRJUserData].isRequestMessage) {
        [[RJUserData defaultRJUserData] updateMessageData];
        [RJUserData defaultRJUserData].isRequestMessage = YES;
    }
    RJHomeVC * homeVC = (RJHomeVC *)_currentVC;
//    [homeVC reloadHomeView];
}
@end
